package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.WmsRecordInDTO;
import com.hxzy.entity.WmsGoods;
import com.hxzy.entity.WmsRecordIn;
import com.hxzy.entity.WmsRespository;
import com.hxzy.entity.WmsSupplier;
import com.hxzy.mapper.WmsGoodsMapper;
import com.hxzy.mapper.WmsRecordInMapper;
import com.hxzy.mapper.WmsRespositoryMapper;
import com.hxzy.mapper.WmsSupplierMapper;
import com.hxzy.service.WmsRecordInService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.WmsRecordInVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class WmsRecordInServiceImpl implements WmsRecordInService {
    @Override
    public BootstrapTableVO search(WmsRecordInDTO dto) {
        BootstrapTableVO tableVO = new BootstrapTableVO();
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRecordInMapper mapper = session.getMapper(WmsRecordInMapper.class);
            WmsSupplierMapper Supplier = session.getMapper(WmsSupplierMapper.class);
            WmsGoodsMapper Goods = session.getMapper(WmsGoodsMapper.class);
            WmsRespositoryMapper Respository = session.getMapper(WmsRespositoryMapper.class);
            PageHelper.startPage(dto.getPageNumber(),dto.getPageSize());
            List<WmsRecordIn> search = mapper.search(dto);

            PageInfo<WmsRecordIn> pg = new PageInfo<>(search);
            tableVO.setTotal(pg.getTotal());
            List<WmsRecordInVO> arr = new ArrayList<>();
            for (WmsRecordIn wmsRecordIn : search) {
                WmsRecordInVO wmsRecordInVO = new WmsRecordInVO();
                BeanUtils.copyProperties(wmsRecordInVO,wmsRecordIn);

                WmsSupplier wmsSupplier = Supplier.selectByPrimaryKey(wmsRecordIn.getRecordSupplierid());
                WmsGoods wmsGoods = Goods.selectByPrimaryKey(wmsRecordIn.getRecordGoodid());
                WmsRespository wmsRespository = Respository.selectByPrimaryKey(wmsRecordIn.getRecordRepositoryid());

                wmsRecordInVO.setSupplierName(wmsSupplier.getSupplierName());
                wmsRecordInVO.setGoodName(wmsGoods.getGoodName());
                wmsRecordInVO.setRepoAddress(wmsRespository.getRepoAddress());

                arr.add(wmsRecordInVO);
            }
            tableVO.setRows(arr);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return tableVO;
    }

    @Override
    public boolean insert(WmsRecordIn record) {
        int count = 0;
        try(SqlSession session = MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRecordInMapper mapper = session.getMapper(WmsRecordInMapper.class);
            count = mapper.insert(record);
            session.commit();
        }
        return count>0;
    }
}
